﻿import { DataContextFactory } from '../domain/fbsDataContext';
import NodeCache = require("node-cache");
const resourceQueue = new NodeCache();

export class InventoryService {
    constructor() { }

    async GetAll() {
        let ctx = DataContextFactory.GetDataContext();
        return await ctx.Inventory.ToList();
    }

    async GetInventoryResourceUrl(storeId: string): Promise<{ url: string }[]> {
        let ctx = DataContextFactory.GetDataContext();
        // TODO : 根据storeId 查询商品 .Where(x => x.store.id == storeId, ["storeId"], [storeId])
        let inventories = await ctx.Inventory.ToList();
        let result = [];
        for (let item of inventories) {
            result.push({
                url: "api/dataSync/inventory/" + storeId + "/" + item.id,
                type: "inventory"
            });

            let r = await this.GetSoldOutResourceUrl(storeId, item.id);
            if (r) result.push(r);
        }

        return result;
    }

    async GetSoldOutResourceUrl(storeId: string, inventoryId: string) {
        let ctx = DataContextFactory.GetDataContext();
        let store = await ctx.Store.First();
        let soldOutList = await ctx.SoldOut.Where(x => x.inventoryId == inventoryId, ["inventoryId"], [inventoryId]).OrderByDesc(x => x.operTime).ToList();
        //当前业务日时间点
        let d = new Date(Date.now());
        d.setHours(4);
        d.setMinutes(0);
        d.setSeconds(0);
        d.toString();
        store.setting.businessDuration = store.setting.businessDuration ? store.setting.businessDuration : 0;

        soldOutList = soldOutList.filter(x => {
            d.setHours(store.setting.businessDuration);
            if (x.operTime > d.getTime()) return true;
            else return false;
        });

        if (soldOutList && soldOutList.length) {
            let soldOut = soldOutList[0];
            return {
                url: "api/dataSync/soldout/" + storeId + "/" + soldOut.id,
                type: "soldout"
            }
        }
    }

    async GetCategoryResourceUrl(storeId: string): Promise<{ url: string }[]> {
        let ctx = DataContextFactory.GetDataContext();
        // TODO： Where(x => x.store.id == storeId, ["storeId"], [storeId])
        let categories = await ctx.Category.ToList();
        let result = [];
        for (let item of categories) {
            result.push({
                url: "api/dataSync/category/" + storeId + "/" + item.id,
                type: "category"
            });
        }

        return result;
    }

    async GetModifierResourceUrl(storeId: string) {
        let ctx = DataContextFactory.GetDataContext();
        let modifiers = await ctx.Modifier.Where(x => x.storeId == storeId, ["storeId"], [storeId]).ToList();
        let result = [];
        for (let item of modifiers) {
            result.push({
                url: "api/dataSync/modifier/" + storeId + "/" + item.id,
                type: "modifier"
            });
        }
        return result;
    }

    GetInventoryById(id: string) {
        let ctx = DataContextFactory.GetDataContext();
        return ctx.Inventory.First(x => x.id == id, ["id"], [id]);
    }

    GetCategoryById(id: string) {
        let ctx = DataContextFactory.GetDataContext();
        return ctx.Category.First(x => x.id == id, ["id"], [id]);
    }

    GetModifierById(id: string) {
        let ctx = DataContextFactory.GetDataContext();
        return ctx.Modifier.First(x => x.id == id, ["id"], [id]);
    }

    GetSoldOutById(id: string) {
        let ctx = DataContextFactory.GetDataContext();
        return ctx.SoldOut.First(x => x.id == id, ["id"], [id]);
    }

    GetStoreInfo(storeId: string) {
        let ctx = DataContextFactory.GetDataContext();
        return ctx.Store.First(x => x.id == storeId, ["storeId"], [storeId]);
    }

    async GetInventoriesByIds(inventoryIds: string[]) {
        let ctx = DataContextFactory.GetDataContext();
        let inventories = await ctx.Inventory.Contains(
            x => x.id,
            inventoryIds
        ).ToList();
        return inventories;
    }
}